<template>
  <div class="cc-home-nav-container">
    <div class="cc-home-nav-container-logo">
      <div class="cc-home-nav-container-logo-text">
        CC
      </div>
      <div>
        <MenuComponent :user="user"></MenuComponent>
      </div>
    </div>
    <div class="cc-home-nav-container-right">
      <el-dropdown @command="changeDropDownMenu">
        <div class="cc-home-nav-container-right-user pointer">
          {{ user.nickName }}
        </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item v-for="(item, key) in dropDownMenu" :key="`dropdown${key}`" :command="item.command">{{ item.name }}</el-dropdown-item>
            </el-dropdown-menu>
          </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script lang="ts">
import {computed, defineComponent, reactive, ref} from 'vue';
import {useStore} from 'vuex';
import {logout, User} from '@/model/UserClass';
import {DropDownMenuItem, MenuItem, MoreCommandEnum} from '@/model/MenuSettingClass';
import ccRouter from '@/router';
import {RouteNameEnum} from '@/model/RouterClass';
import {apiMethod} from '@/apis/apis';

export default defineComponent({
  name: 'HomeNav',
  setup() {
    let dropDownMenu = reactive<DropDownMenuItem[]>([
      {name: '切换用户', command: MoreCommandEnum.commandChangeUser},
      {name: '个人中心', command: MoreCommandEnum.commandPersonalCenter},
      {name: '注销登录', command: MoreCommandEnum.commandLogout}]);
    let changeDropDownMenu: (command: MoreCommandEnum) => void = (command: MoreCommandEnum) => {
      if (MoreCommandEnum.commandLogout === command) {
        logout();
      } else if (MoreCommandEnum.commandPersonalCenter === command) {
        ccRouter.push({routeName: RouteNameEnum.personalCenter})
      } else if (MoreCommandEnum.commandChangeUser === command) {
        apiMethod.apiTestList();
      }
    }
    let user = computed((): User => useStore().getters['user/user'] || reactive(new User()));
    return {user, dropDownMenu, changeDropDownMenu};
  }
});

</script>

<style lang="scss" scoped>
.cc-home-nav {
  &-container {
    background: #001938;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;

    &-logo {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;

      &-text {
        padding-left: 20px;
        font-size: 22px;
        line-height: 25px;
        color: #ffffff;
      }
    }

    &-right {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      padding-right: 20px;
      &-user {
        font-size: 18px;
        line-height: 40px;
        color: #ffffff;
        font-weight: 600;
      }
    }
  }
}
</style>
